package com.rj.gpdata.domain.res.kline;

import com.rj.util.DateUtils;
import lombok.Data;

import java.util.*;
import java.util.function.BiConsumer;

/**
 * K线核心数据对象
 */
@Data
public class KlineData extends MetaInfo {
    /**
     * 转换后的列表
     */
    private List<String> klines;
    /**
     * 转换后的列表
     */
    private List<Map> diff;
    /**
     * 转换后的列表
     */
    private List<Map> klineItems;


    public   List<Map> convertFields2ToKlineItems(String fields2,String securityCode) {
        if(klineItems != null){
           return klineItems;
        }
        if (fields2 == null) {
            return null;
        }
        klineItems=new ArrayList<>();
        int idx = 0;
        Map<Integer, BiConsumer<String,  Map<String, Object>>> valMap = new HashMap<>(16);
        for (String item : fields2.split(",")) {
            switch (item) {
                case "f51" -> valMap.put(idx++, (val, k) -> {
                    k.put("RQ", val);
                });
                case "f52" -> valMap.put(idx++, (val, k) -> {
                    k.put("OPEN", Double.parseDouble(val));
                });
                case "f53" -> valMap.put(idx++, (val, k) -> {
                    k.put("CLOSE", Double.parseDouble(val));
                });
                case "f54" -> valMap.put(idx++, (val, k) -> {
                    k.put("HIGH", Double.parseDouble(val));
                });
                case "f55" -> valMap.put(idx++, (val, k) -> {
                    k.put("LOW", Double.parseDouble(val));
                });
                case "f56" -> valMap.put(idx++, (val, k) -> {
                    k.put("VOLUME",Long.parseLong(val));
                });
                case "f57" -> valMap.put(idx++, (val, k) -> {
                    k.put("AMOUNT",Double.parseDouble(val));
                });
                case "f58" -> valMap.put(idx++, (val, k) -> {
                    k.put("AMPLITUDE",Double.parseDouble(val));
                });
                case "f59" -> valMap.put(idx++, (val, k) -> {
                    k.put("CHANGE_PERCENT",Double.parseDouble(val));
                });
                case "f60" -> valMap.put(idx++, (val, k) -> {
                    k.put("CHANGE_AMOUNT",Double.parseDouble(val));
                });
                case "f61" -> valMap.put(idx++, (val, k) -> {
                    k.put("TURNOVER_RATE",Double.parseDouble(val));
                });
                default -> throw new RuntimeException("未转换的"+item);
            }
        }
        return convertFields2ToKlineItems(valMap,securityCode);
    }

    private List<Map> convertFields2ToKlineItems(Map<Integer, BiConsumer<String,  Map<String, Object>>>  mapping, String securityCode) {
            klines.forEach(kline->{
             String[] vals=   kline.split(",");
                Map<String, Object> klineItem = new HashMap<>();
                klineItem.put("SECURITY_CODE",securityCode);
                for (int i = 0; i < vals.length; i++) {
                    mapping.get(i).accept(vals[i],klineItem);
                }
                klineItems.add(klineItem);

            });
        klines=null;
        return klineItems;
    }
}